package com.goktech.controller;

import com.goktech.entity.User;

import com.goktech.service.InformService;
import com.goktech.service.InformTypeService;
import com.goktech.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user/inform")
public class userInformController {

    @Autowired
    private InformTypeService informTypeService;

    @Autowired
    private InformService informService;

    @Autowired
    private User user;

    @Autowired
    private UserService userService;

    @RequestMapping("/all/")
    public String InformSelectAll(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("search", "all");
        return "redirect:add?yeshu=0";
    }

    @RequestMapping("/all/add")
    public String searchAdd(Integer yeshu, HttpServletRequest request) {

        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");
        user.setZhanghao(zhanghao);
        User byUser = userService.findByUser(user);

        List<Map<String, Object>> maps = null;
        maps = informService.userfindByAll(byUser, yeshu * 5, 5);

        if (maps.size() == 0 && yeshu != 0) {
            maps = informService.userfindByAll(byUser, (yeshu - 1) * 5, 5);
        } else {
            yeshu = yeshu + 1;
        }
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        System.out.println("yeshu:" + yeshu);
        return "user/userInform";
    }

    @RequestMapping("/all/delete")
    public String searchDelete(Integer yeshu, HttpServletRequest request) {

        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");
        user.setZhanghao(zhanghao);
        User byUser = userService.findByUser(user);

        List<Map<String, Object>> maps = null;
        if (yeshu > 1) {
            yeshu = yeshu - 1;
        }
        maps = informService.userfindByAll(byUser, (yeshu - 1) * 5, 5);
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        return "user/userInform";
    }


    @RequestMapping("/public/")
    public String publicAll(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("search", "public");
        return "redirect:add?yeshu=0";
    }

    @RequestMapping("/public/add")
    public String publicAdd(Integer yeshu, HttpServletRequest request) {
        List<Map<String, Object>> maps = null;
        maps = informService.adminPublicAll(yeshu * 5, 5);
        if (maps.size() == 0 && yeshu != 0) {
            maps = informService.adminPublicAll((yeshu - 1) * 5, 5);
        } else {
            yeshu = yeshu + 1;
        }
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        return "user/userInform";
    }

    @RequestMapping("/public/delete")
    public String publicDelete(Integer yeshu, HttpServletRequest request) {
        List<Map<String, Object>> maps = null;
        if (yeshu > 1) {
            yeshu = yeshu - 1;
        }
        maps = informService.adminPublicAll((yeshu - 1) * 5, 5);
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        return "user/userInform";
    }

    @RequestMapping("/private/")
    public String InformPrivateAll(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.setAttribute("search", "private");
        return "redirect:add?yeshu=0";
    }

    @RequestMapping("/private/add")
    public String PrivateAdd(Integer yeshu, HttpServletRequest request) {

        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");
        user.setZhanghao(zhanghao);
        User byUser = userService.findByUser(user);

        List<Map<String, Object>> maps = null;
        maps = informService.userfindByPrivate(byUser, yeshu * 5, 5);
        System.out.println("maps:" + maps);
        if (maps.size() == 0 && yeshu != 0) {
            maps = informService.userfindByPrivate(byUser, (yeshu - 1) * 5, 5);
        } else {
            yeshu = yeshu + 1;
        }
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        System.out.println("yeshu:" + yeshu);
        return "user/userInform";
    }

    @RequestMapping("/private/delete")
    public String PrivateDelete(Integer yeshu, HttpServletRequest request) {


        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");
        user.setZhanghao(zhanghao);
        User byUser = userService.findByUser(user);

        List<Map<String, Object>> maps = null;
        if (yeshu > 1) {
            yeshu = yeshu - 1;
        }
        maps = informService.userfindByPrivate(byUser, (yeshu - 1) * 5, 5);
        request.setAttribute("yeshu", yeshu);
        request.setAttribute("inform", maps);
        return "user/userInform";
    }

    @RequestMapping("/select")
    public String select(String text,HttpServletRequest request){
        String text1 = "%"+text+"%";
        HttpSession session = request.getSession();
        String zhanghao = (String) session.getAttribute("zhanghao");
        user.setZhanghao(zhanghao);
        User byUser = userService.findByUser(user);

        List<Map<String, Object>> maps = null;

        maps = informService.userfindSendZhanghao(byUser.getId(), text1);

        request.setAttribute("yeshu", 1);
        request.setAttribute("inform", maps);
        return "user/userInform";
    }
}
